<template>
	<view class="container">
		<view class="cell-group">
			<u-cell-group>
				<u-cell-item icon="account" title="个人信息" @click="editUserInfo()"></u-cell-item>
				<u-cell-item icon="info-circle" title="关于我们" @click="toPage('/pages/user/about-us/about-us')"></u-cell-item>
				<u-cell-item icon="edit-pen" title="意见反馈" @click=""></u-cell-item>
				<u-cell-item icon="level" title="检查更新" @click="checkVersion()" :value="version"></u-cell-item>
			</u-cell-group>
		</view>
		<view class="logout" v-if="isAuthInfo">
			<u-button @click="logoutModalShow = true" type="error" shape="circle">退出登录</u-button>
		</view>
		
		<u-modal
			v-model="logoutModalShow" 
			content="确定退出账号吗？" 
			:show-cancel-button="true"
			@confirm="logout()"
			></u-modal>
			
		<u-modal
			v-model="updateModalShow" 
			content="检查到新版本,建议更新" 
			confirm-text="立即更新"
			:show-cancel-button="true"
			@confirm="update()"
			></u-modal>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import { constant } from '@/utils/constant.js'
	import { getVersion } from "@/api/version.js";
	export default {
		data() {
			return {
				version: '',
				apkDownloadUrl: '',
				updateModalShow: false, // 更新确认模态框是否显示
				isAuthInfo: false, // 是否显示退出按钮
				logoutModalShow: false, // 退出确认模态框是否显示
				userInfo: {}
			}
		},
		methods: {
			// 检查更新
			checkVersion() {
				let that = this; 
				getVersion().then(res => {
					let latestVersion = res.data.version
					console.log("最新版本", latestVersion);
					if(that.version == latestVersion) {
						this.$refs.uToast.show({
							title: '已是最新版本',
							type: 'success'
						})
					} else {
						that.updateModalShow = true
						that.apkDownloadUrl = res.data.apkDownloadUrl
					}
					
				})
			},
			// 更新
			update() {
				let that = this; 
				console.log("开始更新，下载地址", that.apkDownloadUrl)
				// plus.runtime.openURL(that.apkDownloadUrl); // 跳转应用发布平台 
				
				//that.apkDownloadUrl    是apk的下载链接
				var dtask = plus.downloader.createDownload(that.apkDownloadUrl, {}, function ( d, status ) {  
					// 下载完成  
					if ( status == 200 ) {   
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},
							function(error){  
								uni.showToast({
									title: '安装失败',  
									mask: false,  
									duration: 1500  
								})
						})  
					} else {  
						 uni.showToast({  
							title: '更新失败',
							mask: false,  
							duration: 1500  
						 });  
					}    
				});  
				dtask.start(); 
			},
			// 页面跳转
			toPage(url) {
				this.$u.route(url)
			},
			// 导航到用户信息修改页面
			editUserInfo(){
				this.$u.route('/pages/user-edit/user-edit')
			},
			// 退出
			logout() {
				// 清空数据缓存
				uni.removeStorageSync(constant.tokenKey);
				uni.removeStorageSync(constant.userInfo);
				uni.switchTab({
					url: '/pages/user/user'
				});
			},
			
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {
			let that = this;
			that.version = plus.runtime.version
			// 获取本地应用资源版本号
			plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) { 
				console.log("现在版本" + wgtinfo.version)
				that.version = wgtinfo.version
			}); 
			
			let userInfo = uni.getStorageSync(constant.userInfo)
			let token = uni.getStorageSync(constant.tokenKey)
			if (userInfo && token) {
				this.isAuthInfo = true;
			}
		}
	}
</script>

<style lang="scss">
	.logout {
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		width: 100%;
		padding: 30rpx;
	}

</style>
